let requestUrl;

function addHandler() {
	requestUrl = "/equipment_teji/insert";

	$("#teji_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#teji_list").datagrid("getSelected");

	if(rowData) {
		requestUrl = "/equipment_teji/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#type").combobox("setValue", rowData.type);
		$("#note").textbox("setValue", rowData.note);
		$("#fennu").numberspinner("setValue", rowData.fennu);
		$("#score").numberspinner("setValue", rowData.score);

		$("#teji_dialog").dialog('open');
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function imageHandler() {
	let rowData = $("#teji_list").datagrid("getSelected");

	if (rowData) {
		$("#teji_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录", "warning");
	}
}

$(document).ready(function() {

	$("#name").textbox({
		width: 120,
		required: true
	});

	fetchSelect("#type", "/equipment_teji_type/selectAll", {
		width: 120,
		required: true
	});

	$("#score").numberspinner({
        min: 0,
        max: 120,
		width: 120,
        editable: true,
        required: true
    });

	$("#fennu").numberspinner({
        min: 0,
        max: 150,
		width: 120,
        editable: true,
        required: true
    });

	$("#note").textbox({
		width: 320,
    	height: 100,
    	required: true,
		multiline: true
	});
	
	$("#teji_dialog").dialog({
		title: "特技信息",
		width: 440,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#teji_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(response) {
						showMsg(response.message);

						$(selector).form("clear");
						$("#teji_dialog").dialog("close");
						$("#teji_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#teji_dialog").dialog("close");
				$("#teji_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#teji_list", "/equipment_teji/upload");

	fetchUploadDialog("/equipment_teji/updateImageById", function () {
		$("#teji_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#teji_list").datagrid({
		url: "/equipment_teji/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", title: "编号", align: "center"},
			{field: "name", title: "特技名称", align: "center", width: 50},
			{field: "image", title: "特技图片", align: "center", width: 50
				, formatter: function(value) {
					if (value) {
						return "<img height='" + size + "' src='" + value + "' />";
					} else {
						return "/";
					}
				}
			},
			{field: "score", title: "特技评分", align: "center", width: 50, sortable: true},
			{field: "type", title: "特技类型", align: "center", width: 50
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/equipment_teji_type/selectById", {
						id: value
					}, function (resp) {
						let data = resp.data;

						$("#teji_type_" + rowIndex).html(data.name);
					}, error);

					return "<div id='teji_type_" + rowIndex + "'></div>";
				}
			},
			{field: "note", title: "特技说明", align: "center", width: 200
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			},
			{field: "fennu", title: "消耗愤怒", align: "center", width: 50, sortable: true
				, formatter: function(value) {
					return value + "点";
				}
			}
		]]
	});

});